<template>
  <div class="dashboard-container">
    <div class="app-container">
      <page-tools :show-before="true">
        <span slot="before">共166条记录</span>
        <template slot="after">
          <el-button size="small" type="warning" @click="$router.push('/import')">导入</el-button>
          <el-button size="small" type="danger" @click="exportExcel">导出</el-button>
          <el-button size="small" type="primary" @click="showDialog=true">新增员工</el-button>
        </template>
      </page-tools>
      <!-- 放置表格和分页 -->
      <el-card>
        <el-table border :data="userList">
          <el-table-column label="序号" sortable="" type="index" />
          <el-table-column label="姓名" sortable="" prop="username" />
          <el-table-column label="头像" sortable="" width="200">
            <template #default="{row}">
              <img v-imgerr="require('@/乌拉.jpg')" :src="row.staffPhoto" alt="" class="avatar" @click="showAvatar(row.staffPhoto)">
            </template>
          </el-table-column>
          <el-table-column label="工号" sortable="" prop="workNumber" />
          <el-table-column label="聘用形式" sortable="" prop="formOfEmployment" :formatter="formOfEmployment" />
          <el-table-column label="部门" sortable="" prop="departmentName" />
          <el-table-column label="入职时间" sortable="">
            <template #default="{row}">
              {{ row.timeOfEntry | formatDate }}
            </template>
          </el-table-column>
          <el-table-column label="账户状态" sortable="">
            <template #default="{row}">
              <el-switch :value="row.enableState===1" />
            </template>
          </el-table-column>
          <el-table-column label="操作" sortable="" fixed="right" width="280">
            <template #default="{row}">
              <el-button type="text" size="small" @click="$router.push(`/employees/detail/${row.id}`)">查看</el-button>
              <el-button type="text" size="small">转正</el-button>
              <el-button type="text" size="small">调岗</el-button>
              <el-button type="text" size="small">离职</el-button>
              <el-button type="text" size="small" @click="assignRole(row.id)">角色</el-button>
              <el-button type="text" size="small" @click="del(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <el-pagination
            layout="prev, pager, next"
            :page-size="page.size"
            :total="page.total"
            @current-change="currentChange"
          />
        </el-row>
      </el-card>
      <add-employees :show-dialog="showDialog" @showAdd="showAdd" />
      <el-dialog title="头像" :visible="isShowAvatar" @close="btnCancel">
        <el-row type="flex" justify="center">
          <canvas ref="myAvatar" />
        </el-row>
      </el-dialog>
      <!-- 分配角色 -->
      <assignRole ref="assignRole" :show-assign="showAssign" @isshowAssign="isshowAssign" />
    </div>
  </div>
</template>

<script>
import { getSysUser, delEmployee } from '@/api/employees'
import employees from '@/api/constant/employees'
import { formatDate } from '@/filters'
import { export_json_to_excel } from '@/vendor/Export2Excel'
import addEmployees from './components/add-employees.vue'
import QRCode from 'qrcode'
import assignRole from './components/assign-role.vue'
export default {
  // filters: {
  //   FormatData(olb) {
  //     return olb.split('T')[0]
  //   }
  // },
  components: {
    addEmployees, assignRole
  },
  data() {
    return {
      userList: [],
      page: {
        page: 1, // 当前页码
        size: 5,
        total: 0 // 总数
      },
      showDialog: false,
      isShowAvatar: false,
      showAssign: false

    }
  },
  created() {
    this.getSysUser()
  },
  methods: {
    assignRole(id) {
      this.showAssign = true
      this.$refs.assignRole.getUserDetailById(id)
    },
    showAvatar(url) {
      // console.log(94)
      if (url) {
        // console.log(96)
        this.isShowAvatar = true
        // console.log(url)
        this.$nextTick(() => {
          // console.log(100)
          QRCode.toCanvas(this.$refs.myAvatar, url, {
            width: 300,
            color: {
              dark: '#66b1ff'
            }
          })
        })
      }
    },
    showAdd() {
      this.showDialog = false
    },
    async del(id) {
      await this.$confirm('是否删除')
      await delEmployee(id)
      this.$message.success('删除成功')
      this.getSysUser()
    },
    async exportExcel() {
      const { rows } = await getSysUser({ page: 1, size: this.page.total })
      console.log(rows)
      const header = ['手机号',
        '姓名',
        '入职日期',
        '聘用形式',
        '转正日期',
        '工号',
        '部门'
      ]
      const data = rows.map(user => {
        return this.userArr(user, header)
      })
      // console.log(data, 93)
      export_json_to_excel({
        header,
        data
      })
    },
    userArr(user, header) {
      const arr = []
      const dict = {
        '姓名': 'username',
        '手机号': 'mobile',
        '入职日期': 'timeOfEntry',
        '聘用形式': 'formOfEmployment',
        '转正日期': 'correctionTime',
        '工号': 'workNumber',
        '部门': 'departmentName'
      }
      console.log(user)
      header.forEach(key => {
        const enkey = dict[key]
        let value = user[enkey]
        if (enkey === 'timeOfEntry' || enkey === 'correctionTime') {
          value = formatDate(value)
        }
        if (enkey === 'formOfEmployment') {
          const obj = employees.hireType.find(item => item.id === value)
          value = obj ? obj.value : '未知'
        }
        arr.push(value)
      })
      return arr
    },
    async getSysUser() {
      const { rows, total } = await getSysUser(this.page)
      console.log(rows, total)
      this.userList = rows
      this.page.total = total
    },
    currentChange(newPage) {
      this.page.page = newPage
      this.getSysUser()
    },
    formOfEmployment(rol, column, cell, index) {
      // const hireType = employees.hireType
      // console.log(hireType, 77)
      const obj = employees.hireType.find(item => item.id === cell)
      return obj ? obj.value : '未知'
    },
    btnCancel() {
      this.isShowAvatar = false
    },
    isshowAssign() {
      this.showAssign = false
    }
  }
}
</script>

<style lang="scss" scoped>
.avatar {
  width: 180px;
  height: 140px;
  object-fit: cover;
}
</style>
